//
//  CellViewController.m
//  BMS
//
//  Created by 颜魏 on 2024/12/9.
//

#import "CellViewController.h"
#import "CellTableViewCell.h"
#import "BTManager.h"

@interface CellViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *cellStatusView;
@property (weak, nonatomic) IBOutlet UIButton *cellStatusBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableHeight;

@property (nonatomic, strong) BTManager *btManager;

@property (nonatomic, strong) NSMutableArray    *finalserialBalance;
@property (nonatomic, strong) NSMutableArray    *finalserialVoltageCount;
@property (nonatomic, strong) NSMutableArray    *finalserialVoltageValue;

@end

@implementation CellViewController
{
    int serialBalance[32];
    int serialVoltage[32];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cellStatusView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.cellStatusView.layer.shadowOffset = CGSizeMake(0, 3);
    self.cellStatusView.layer.shadowOpacity = 0.7;
    self.cellStatusView.layer.shadowRadius = 5;
    self.finalserialBalance = [[NSMutableArray alloc] init];
    self.finalserialVoltageCount = [[NSMutableArray alloc] init];
    self.finalserialVoltageValue = [[NSMutableArray alloc] init];
    [self.finalserialVoltageCount addObjectsFromArray:@[@"1", @"2", @"3", @"4"]];
    [self.finalserialVoltageValue addObjectsFromArray:@[@"0.00", @"0.00", @"0.00", @"0.00"]];
    self.tableHeight.constant = 58*self.finalserialVoltageCount.count;
    self.btManager = [BTManager sharedBTManager];

    [self setLanguage];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReciveData:) name:kBTDidReciveData object:nil];

    [self.tableview registerNib:[UINib nibWithNibName:@"CellTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellTableViewCell"];
}

- (void)setLanguage {
    [self.cellStatusBtn setTitle:[NSString stringWithFormat:@"  %@", NSLocalizedString(@"Cell status", nil)] forState:UIControlStateNormal];
}

- (IBAction)actionReconnect:(id)sender {
    [self.btManager reConnectPeripheral];
}

- (IBAction)actionDisconnect:(id)sender {
    [self.btManager disconnectRootPeripheral];
    [self.finalserialVoltageCount removeAllObjects];
    [self.finalserialVoltageValue removeAllObjects];
    [self.finalserialVoltageCount addObjectsFromArray:@[@"1", @"2", @"3", @"4"]];
    [self.finalserialVoltageValue addObjectsFromArray:@[@"0.00", @"0.00", @"0.00", @"0.00"]];
    self.tableHeight.constant = 58*self.finalserialVoltageCount.count;
}

#pragma mark - notification
- (void)didReciveData:(NSNotification *)notification {
    NSData *reciveData = notification.object;
    Byte *dataBytes = (Byte*)reciveData.bytes;
    if (dataBytes[2]==0x4c) {
        //3.1 主控读取电池电压数据
        for (int t=0;t<32;t++) {
            int voltage = dataBytes[3+t*2]*256 + dataBytes[3+t*2+1];
            serialVoltage[t] = voltage;
        }
    }
    if ((dataBytes[2]==0x32) & (reciveData.length>30)) {
        //3.2 主控读取电池温度电流等信息
        //1-16 均衡状态
        int serialBalance16 = dataBytes[3+0x17*2]*256 + dataBytes[3+0x17*2+1];
        //17-32 均衡状态
        int andbit = 1;
        int andValue = 1;
        for(int t = 0; t < 16; t++) {
            andValue = serialBalance16 & andbit;
            if (andValue > 0) {
                serialBalance[t] = 1;
            }else {
                serialBalance[t] = 0;
            }
            andbit = andbit<<1;
        }
        [self.finalserialBalance removeAllObjects];
        [self.finalserialVoltageCount removeAllObjects];
        [self.finalserialVoltageValue removeAllObjects];
        for (int t = 0; t < 16; t++) {
            if (serialVoltage[t] < 61000) {
                [self.finalserialVoltageCount addObject:[NSString stringWithFormat:@"%i", t+1]];
                [self.finalserialBalance addObject:[NSString stringWithFormat:@"%i", serialBalance[t]]];
                [self.finalserialVoltageValue addObject:[NSString stringWithFormat:@"%i", serialVoltage[t]]];
            }
        }
        self.tableHeight.constant = 58*self.finalserialVoltageCount.count;
        [self.tableview reloadData];
    }
}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.finalserialVoltageCount.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellTableViewCell"];
    cell.index = indexPath.item;
    cell.numV = [self.finalserialVoltageValue[indexPath.item] description];
    cell.isLast = indexPath.row == self.finalserialVoltageValue.count - 1;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

@end
